package com.gzw.shop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gzw.shop.R;
import com.gzw.shop.activity.CreateOrderActivity;
import com.gzw.shop.adapter.CartAdapter;
import com.gzw.shop.adapter.DividerItemDecoration;
import com.gzw.shop.bean.ShoppingCart;
import com.gzw.shop.utils.CartProvider;
import com.gzw.shop.widget.GToolbar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CartFragment extends BaseFragment {

    @BindView(R.id.toolbar)
    GToolbar mToolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.checkbox_all)
    CheckBox checkboxAll;
    @BindView(R.id.txt_total)
    TextView tvTotal;
    @BindView(R.id.btn_order)
    Button btnOrder;
    @BindView(R.id.btn_del)
    Button btnDel;

    private CartAdapter mAdapter;
    private CartProvider mProvider;

    private static final int ACTION_EDIT = 1;
    private static final int ACTION_CAMPLATE = 2;

    List<ShoppingCart> carts;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_cart;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    @Override
    public void initData() {
        mProvider = new CartProvider(getActivity());
        showData();
    }

    private void initToolbar() {

        mToolbar.setRightBtnTag(ACTION_EDIT);
        mToolbar.setRightBtnOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int action = (int) view.getTag();
                if (action == ACTION_EDIT) {
                    showDelControl();
                } else if (action == ACTION_CAMPLATE) {
                    hideDelControl();
                }
            }
        });

        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAdapter.delCart();
            }
        });

    }

    //展示删除按钮
    private void showDelControl() {

        mToolbar.setRightBtnText("完成");
        tvTotal.setVisibility(View.GONE);
        btnOrder.setVisibility(View.GONE);
        btnDel.setVisibility(View.VISIBLE);
        mToolbar.setRightBtnTag(ACTION_CAMPLATE);
        mAdapter.checkAll(false);
        checkboxAll.setChecked(false);
    }

    //隐藏删除按钮
    private void hideDelControl() {

        mToolbar.setRightBtnText("编辑");
        tvTotal.setVisibility(View.VISIBLE);
        btnOrder.setVisibility(View.VISIBLE);
        btnDel.setVisibility(View.GONE);
        mToolbar.setRightBtnTag(ACTION_EDIT);
        mAdapter.checkAll(true);
        checkboxAll.setChecked(true);
    }


    //展示数据
    public void showData() {
        carts = mProvider.getAll();
        if (carts.size() != 0) {
            checkboxAll.setChecked(true);
        } else {
            checkboxAll.setChecked(false);
        }
        if (mAdapter == null) {
            mAdapter = new CartAdapter(getActivity(), carts, checkboxAll, tvTotal);
            recyclerView.setAdapter(mAdapter);
            mAdapter.showTotalPrice();
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        } else {
            mAdapter.clearData();
            mAdapter.addData(carts);
            mAdapter.showTotalPrice();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
//        showData();
    }

    /**
     * 结算
     */
    @OnClick(R.id.btn_order)
    public void toOrder() {
        Intent intent = new Intent(getActivity(), CreateOrderActivity.class);
        startActivity(intent, true);
    }
}
